﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIapp.Interfaces;
using GSQ.BusinessEntities;
using GSQ.BusinessLogic;
using Telerik.WinControls.UI;
using System.Collections.ObjectModel;
namespace UIapp.Views
{
    public partial class frmBancos : Plantillas.frmViewPlantilla, Interfaces.IPantalla
    {
        private bool newRecord;
        private List<BANCOS> lista = new List<BANCOS>();

        public frmBancos()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;            
            this.lista = new BancosBL().GetAll();
        }

        public int ComandosSoportados
        {
            get
            {
                return (int)ComandosPantalla.Nuevo |
                       (int)ComandosPantalla.Editar |
                       (int)ComandosPantalla.Refrescar |
                       (int)ComandosPantalla.Buscar;
            }
        }

        public void Nuevo()
        {
            this.bANCOSRadGridView.Rows.AddNew();
            this.bANCOSRadGridView.Enabled = false;
            this.splitPanel2.Collapsed = false;
            this.bANCORadTextBox.Focus();
            this.newRecord = true;
        }

        public void Editar()
        {
            if (this.bANCOSRadGridView.SelectedRows.Count > 0)
            {
                this.bANCOSRadGridView.Enabled = false;
                this.splitPanel2.Collapsed = false;
                this.bANCORadTextBox.Focus();
                this.newRecord = false;
            }
        }

        public void Eliminar()
        {
            throw new NotImplementedException();
        }

        public void Refrescar()
        {
            throw new NotImplementedException();
        }

        public void Buscar()
        {
            throw new NotImplementedException();
        }

        private void AplicarCambios()
        {
            this.lista[this.bANCOSBindingSource.Position].BANCO = this.bANCORadTextBox.Text.Trim();
            this.lista[this.bANCOSBindingSource.Position].ACTIVO = this.aCTIVORadCheckBox.Checked;
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            try
            {
                this.AplicarCambios();
                if ((this.bANCOSRadGridView.CurrentRow.DataBoundItem as BANCOS).HasChange)
                {
                    if (this.newRecord)
                    {
                        new BancosBL().Insert((BANCOS)this.bANCOSRadGridView.CurrentRow.DataBoundItem);
                    }
                    else
                    {
                        new BancosBL().Update((BANCOS)this.bANCOSRadGridView.CurrentRow.DataBoundItem,
                                              (BANCOS)this.bANCOSRadGridView.CurrentRow.DataBoundItem);
                    }
                }
                this.bANCOSRadGridView.Enabled = true;
                this.splitPanel2.Collapsed = true;
            }
            catch (Exception ex)
            {
                Comun.ShowMessage(ex);
            }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            if (this.newRecord)
            {
                this.bANCOSRadGridView.CurrentRow.Delete();

            }
            this.splitPanel2.Collapsed = true;
            this.bANCOSRadGridView.Enabled = true;
        }

        private void frmBancos_Load(object sender, EventArgs e)
        {
            this.bANCOSBindingSource.DataSource = this.lista;
        }
    }
}
